﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Windows;
using System.Xml;

namespace Fulcrum.ControlGenerator
{
    /// <summary>
    /// Configuration data about a given dependency property
    /// </summary>
    class DependencyPropertyXmlConfiguration : BaseDependencyPropertyXmlConfiguration, Fulcrum.ControlGenerator.IDependencyPropertyConfiguration
    {
        public DependencyPropertyXmlConfiguration(XmlReader xmlReader)
            : base(xmlReader)
        {
            this.Summary = xmlReader.GetAttribute("summary");
            this.Remarks = xmlReader.GetAttribute("remarks");
            this.Description = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "description", this.Summary);
            this.ProvideSetter = XmlReaderHelp.GetOptionalBoolAttributeValue(xmlReader, "provideSetter", true);

            this.TypeConverter = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "typeConverter", String.Empty);

            ProcessLocalizationSettings(xmlReader);

            xmlReader.Read();
        }

        private void ProcessLocalizationSettings(XmlReader xmlReader)
        {
            var defaultIsLocalizable = IsLocalizedType(this.Type);

            var isLocalizable = XmlReaderHelp.GetOptionalBoolAttributeValue(xmlReader, "localizable", defaultIsLocalizable);

            var defaultCategory = "None";
            var defaultModifiability = "Unmodifiable";
            var defaultReadability = "Unreadable";

            if (isLocalizable)
            {
                defaultCategory = "Text";
                defaultModifiability = "Modifiable";
                defaultReadability = "Readable";
            }

            var localizationCategory = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "localizationCategory", defaultCategory);
            var localizationModifiability = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "localizationModifiability", defaultModifiability);
            var localizationReadability = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "localizationReadability", defaultReadability);

            this.LocalizationCategory = (LocalizationCategory) Enum.Parse(typeof(LocalizationCategory), localizationCategory);
            this.LocalizationModifiability = (Modifiability) Enum.Parse(typeof(Modifiability), localizationModifiability);
            this.LocalizationReadability = (Readability) Enum.Parse(typeof(Readability), localizationReadability);
        }

        bool IsLocalizedType(string typeName)
        {
            if (typeName.Equals("string", StringComparison.OrdinalIgnoreCase) ||
                typeName.Equals("char", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string TypeConverter
        {
            get;
            private set;
        }

        public string Summary
        {
            get;
            private set;
        }

        public string Remarks
        {
            get;
            private set;
        }

        public string Description
        {
            get;
            private set;
        }

        /// <summary>
        /// This is needed because a RO dependency property which is a collection can't be have items
        /// provided to it via XAML if there is a setter (even if it is private).
        /// This appears to be an issue w/ the compiler/XAML engine.
        /// </summary>
        public bool ProvideSetter
        {
            get;
            private set;
        }

        public LocalizationCategory LocalizationCategory
        {
            get;
            private set;
        }

        public Modifiability LocalizationModifiability
        {
            get;
            private set;
        }

        public Readability LocalizationReadability
        {
            get;
            private set;
        }
    }
}
